﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 插入备注文本块命令。
    /// ★①，修改类名。
    /// </summary>
    public static class InsertSummaryCommentCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static InsertSummaryCommentCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "InsertSummaryCommentCommand",
                "InsertSummaryCommentCommand",
                typeof(InsertSummaryCommentCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.M,ModifierKeys.Control,"Ctrl+M|Ctrl+D"),
                    new KeyGesture(Key.D,ModifierKeys.Control,"Ctrl+D"),
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            //如何插入一个图片？？？

            AddSummaryComment();
        }

        public static void AddSummaryComment(Point? point = null)
        {
            var result = CanExecute();
            if (result != string.Empty)
            {
                MessageBox.Show(result, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Title mainSelectedTitle = null;

            if (Globals.MainWindow.SelectedTitlesList.Count <= 0)
            {
                if (Globals.MainWindow.SelectedTitleImage == null) return;

                mainSelectedTitle = Globals.MainWindow.SelectedTitleImage.MasterTitle;
            }

            foreach (Title t in Globals.SelectedTitlesList)
            {
                if (t.IsMainSelectedTitle)
                {
                    mainSelectedTitle = t; break;
                }
            }

            if (mainSelectedTitle == null) return;
            if (mainSelectedTitle.IsFixed) return;

            Point selCenter = new Point(mainSelectedTitle.LeftTop.X + mainSelectedTitle.ActualWidth / 2,
                mainSelectedTitle.LeftTop.Y + mainSelectedTitle.ActualHeight / 2);

            Comment cmt = new Comment(Globals.MainWindow, mainSelectedTitle);


            XmlNode commentSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("CommentSet");
            if (commentSetNode == null)
            {
                commentSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.AppendXml("<CommentSet />");
            }

            XmlNode newData;

            if (point != null)
            {
                Point pt = (Point)point;

                Point newLocation;

                if (cmt.Editor.RootTitle == cmt.MasterTitle)
                {
                    newLocation = pt;
                }
                else
                {
                    newLocation = new Point(pt.X - selCenter.X, pt.Y - selCenter.Y);
                }

                cmt.Location = newLocation;

                newData = commentSetNode.AppendXml("<Comment MasterTitleID=\"" +
                    mainSelectedTitle.ID + "\" Location=\"" + newLocation.ToString() +
                    "\"><TextSet><Text>..概要文本..</Text></TextSet></Comment>");
            }
            else
            {
                Point baseLocation = new Point(selCenter.X, selCenter.Y - 100 - mainSelectedTitle.ActualHeight / 2);

                newData = commentSetNode.AppendXml("<Comment MasterTitleID=\"" + mainSelectedTitle.ID +
                    "\" IsSummary=\"True\" Location=\"" + baseLocation.ToString() +
                    "\" ><TextSet><Text>..概要文本..</Text></TextSet></Comment>");
            }

            cmt.XmlData = newData;
            cmt.RebuildANewID();

            Globals.MainWindow.mainCanvas.Children.Add(cmt);
            Globals.MainWindow.mainCanvas.Children.Add(cmt.ConnectLine);

            ModifingItem mi = new ModifingItem()
            {
                ModifingName = "添加概要",
                IsNeedRefreshTitleAndLinesLocation = true//概要是参与自动布局的。
            };

            if (cmt.MasterTitle != null && cmt.MasterTitle == Globals.MainWindow.RootTitle)
            {
                cmt.TextForeground = Brushes.DarkGreen;
                cmt.BorderColor = Brushes.DarkViolet;
                cmt.HideConnectLine = true;
            }

            Action actAddComment = new Action(cmt.ID, -1, cmt.GetType().Name, "AddComment", null,
                cmt.XmlData.OuterXml, ActionType.AddComment, cmt.MasterTitleID);

            mi.Add(actAddComment);

            cmt.RefreshLoaction();

            var masterTitle = cmt.MasterTitle;
            if (masterTitle != null) masterTitle.IsNeedSizeInvalid = true;

            Globals.MainWindow.SelectedComment = cmt;
            Globals.MainWindow.Do(mi);
            Globals.MainWindow.RefreshTitleAndLinesLocation();
            Globals.MainWindow.IsModified = true;

        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = (CanExecute() == string.Empty);
        }

        internal static string CanExecute()
        {
            if (Globals.MainWindow.SelectedTitlesList.Count != 1) return "　　只能选定一个标题添加概要！";
            Title t = Globals.SelectedTitlesList[0];

            if (t.HigherLevelTitleID == null || t.HigherLevelTitleID.Length <= 0) return "　　主标题不能添加概要！";

            if (t.IsFixed) return "　　自适应标题不能添加概要！";

            if (t.SummaryComment != null)
            {
                return "　　此标题已存在概要！";
            }

            return string.Empty;
        }
    }
}
